package com.css.dzj.zfjg.modules.other.controller;

import com.alibaba.fastjson.JSON;
import com.css.dzj.zfjg.common.audit.service.SysAuditService;
import com.css.dzj.zfjg.common.util.Constant;
import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.modules.other.entity.JgJgxwOtherActionEntity;
import com.css.dzj.zfjg.modules.other.service.JgJgxwOtherActionService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * 其他行为信息
 *
 * @author liyuan
 */
@RestController
@RequestMapping("jgxw/jgjgxwotheraction")
public class JgJgxwOtherActionController {
    @Autowired
    private JgJgxwOtherActionService jgJgxwOtherActionService;

    // base日志接口
    @Autowired
    SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Integer pageSize, @RequestParam Integer pageNo){
        String s ="";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<JgJgxwOtherActionEntity> page = jgJgxwOtherActionService.queryPage(pageRequest);
            s = JSON.toJSONString(page);
            sysAuditService.saveSuccessAudit("分页查询行政其它行为成功", Constant.ZFJG_JGJGXWOTHERACTION, Constant.PT, Constant.ZFJG_PAGE, s, request);
            return R.ok().put("page", page);
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("分页查询行政其它行为失败", Constant.ZFJG_JGJGXWOTHERACTION, Constant.YZ, Constant.ZFJG_PAGE, s, e, request);
            return R.error(e.getMessage());
        }
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
        String s = "";
        try{
            JgJgxwOtherActionEntity jgJgxwOtherAction = jgJgxwOtherActionService.getById(id);
            s = JSON.toJSONString(jgJgxwOtherAction);
            sysAuditService.saveSuccessAudit("通过id获取行政其它行为信息成功", Constant.ZFJG_JGJGXWOTHERACTION, Constant.PT, Constant.ZFJG_SELECT, s, request);
            return R.ok().put("jgJgxwOtherAction", jgJgxwOtherAction);
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("通过id获取行政其它行为信息失败", Constant.ZFJG_JGJGXWOTHERACTION, Constant.YZ, Constant.ZFJG_SELECT, s, e, request);
            return R.error(e.getMessage());
        }
    }


    /**
     * 添加其他行为
     */
    @RequestMapping("/save")
    public R addOtherAction(@RequestBody JgJgxwOtherActionEntity jgJgxwOtherActionEntity){
        String s = "";
        try{
            jgJgxwOtherActionService.save(jgJgxwOtherActionEntity);
            s =JSON.toJSONString(jgJgxwOtherActionEntity);
            sysAuditService.saveSuccessAudit("行政其它行为添加成功", Constant.ZFJG_JGJGXWOTHERACTION, Constant.PT, Constant.ZFJG_ADD, s, request);
            return R.ok("保存成功!");
        } catch(Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("行政其它行为添加失败", Constant.ZFJG_JGJGXWOTHERACTION, Constant.YZ, Constant.ZFJG_ADD, s, e, request);
            return R.error(e.getMessage());
        }

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody JgJgxwOtherActionEntity jgJgxwOtherAction,HttpServletRequest request){
        String s = "";
        try{
            jgJgxwOtherActionService.updateById(jgJgxwOtherAction);
            s = JSON.toJSONString(jgJgxwOtherAction);
            sysAuditService.saveSuccessAudit("行政其它行为修改成功", Constant.ZFJG_JGJGXWOTHERACTION, Constant.PT, Constant.ZFJG_UPDATE, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("行政其它行为修改失败", Constant.ZFJG_JGJGXWOTHERACTION, Constant.YZ, Constant.ZFJG_UPDATE, s, e, request);
            return R.error(e.getMessage());
        }

    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestParam String id){
        String s  = "";
        try {
            jgJgxwOtherActionService.deleteById(id);
            s =JSON.toJSONString(id);
            sysAuditService.saveSuccessAudit("行政其它行为删除成功", Constant.ZFJG_JGJGXWOTHERACTION, Constant.PT, Constant.ZFJG_DELETE, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("行政其它行为删除失败", Constant.ZFJG_JGJGXWOTHERACTION, Constant.YZ, Constant.ZFJG_DELETE, s, e, request);
            return R.error(e.getMessage());
        }

    }


}

